/*
 * File class provides file io operations between simulation and host os.
 * The file length is unknown before the file is read, so the buffer will
 * be allocated as a zero length String array on construction to prevent 
 * the need for checking if null.  The read method will open the file twice
 * The first read pass is to verify the file can be read and to check the 
 * file size in order to allocate the buffer.  The second read pass will
 * copy the lines into the buffer.
 * Currently only read-only operations are supported.  Write method is an
 * empty stub for future use. 
 * */

package os;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class File {
	// The name of the file.  
	// Note: public so can be changed outside of class, but not advised.  Use constructor.
	public String name;
	
	// a buffer to store the file in line by line.
	public String buffer[];
	
	// Constructor  
	public File( String name ) {
		this.name = name;
		// allocate an empty buffer so it isn't null
		buffer = new String[0];
	}

	public File( String name, int length ) {
		this.name = name;
		buffer = new String[ length ];
	}

	// Read a file completely into the buffer.
	public boolean read( ) {
		String path = name;
		URL url = Thread.currentThread().getContextClassLoader().getResource( path );
		
		//java.io.File jfile;
		//jfile = new java.io.File( name );
		
		// See if file can be read ( and allocate buffer if so otherwise read fails )
		if( !preread( path ) )
			return false;
		
		int i = 0;
		
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
		BufferedReader reader= new BufferedReader(new InputStreamReader( is ));
		
		try{
			//java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.FileReader( jfile ) );
			
			String line = null;
	        while ( ( line = reader.readLine( ) ) != null )
	        	buffer[ i++ ] = line;

	        reader.close( );
		} catch ( Exception ex ) {
			System.err.println( "Read error at line " +  ( i + 1 ) + " on file: " + name );
			// There was some read error in the process.  Buffer may contain some data, but cant be validated
			// Therefore return false for the read
			return false;
		}
		
		// File has been completely read into the buffer; therefore, return true.
		return true;
	}
	
	// Write a file completely from the buffer.
	// Currently no requirements to write, but method stub provided for future use.
	public boolean write( ) {
		java.io.File jfile;
		jfile = new java.io.File( name );
		int i = 0;
		try{
			java.io.BufferedWriter writer = new java.io.BufferedWriter( new java.io.FileWriter( jfile ) );
			for( i = 0; i < buffer.length; i++ ) {
				writer.write( buffer[i] + "\n" );
			}
			writer.close( );
		} catch ( Exception ex ) {
			System.err.println( "Write error at line " +  ( i + 1 ) + " on file: " + name );
			// There was some read error in the process.  Buffer may contain some data, but cant be validated
			// Therefore return false for the read
			return false;
		}
		return true;
	}
	
	// Method to verify that the file can be read and to allocate the buffer.
	private boolean preread( String path ) {
		int lines = 0;
		
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
		BufferedReader reader= new BufferedReader(new InputStreamReader( is ));
		
		try{
	        while ( reader.readLine( ) != null )
	        	lines++;
	        
	        reader.close( );
		} catch ( Exception ex ) {
			System.err.println( "File cannot be read: " + name );
			// Cannot read from the file so method returns false
			return false;
		}
		
		// Now know how long buffer should be so allocate it
		buffer = new String[ lines ];
		// Reading can begin so method returns true
		return true;
	}
	/*
	private boolean preread( java.io.File jfile ) {
		int lines = 0;
		
		try{
			java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.FileReader( jfile ) );
			
	        while ( reader.readLine( ) != null )
	        	lines++;
	        
	        reader.close( );
		} catch ( Exception ex ) {
			System.err.println( "File cannot be read: " + name );
			// Cannot read from the file so method returns false
			return false;
		}
		
		// Now know how long buffer should be so allocate it
		buffer = new String[ lines ];
		// Reading can begin so method returns true
		return true;
	}
	*/
}
